Rails: Generic form actions, cancel link losing `:back` on validation failure

Posted by Patrick Connor on Stack Overflow See other posts from Stack Overflow or by Patrick Connor
Published on 2011-01-16T21:36:54Z Indexed on 2011/01/17 6:53 UTC
Read the original article Hit count: 152

Filed under:

I am trying to create a generic set of Submit, Cancel, and Destroy actions for forms. At this point, it appears that everything is working, except that I lose :back functionality then a form reloads due to validation errors. Is there a way to catch the fact that validation has failed, and in that case, keep the request.env['HTTP_REFERER'] or :back value the same without having to edit every controller?

= simple_form_for @announcement do |f|
  = f.error_notification
  = f.input :message
  = f.input :starts_at
  = f.input :ends_at
  #submit
    = f.button :submit
    = "or "
    = link_to("cancel", url_for(:back))
    .right
      - if !f.object.new_record?
        - resource = (f.object.class.name).downcase
        = link_to "destroy", url_for(:action => 'destroy'), :confirm => "Are you sure that you want to delete this #{resource}?", :method => :delete
  .clear
  .non_input
    #post_back_msg
      #indicator.inline
        = image_tag "indicator.gif"
      .inline
        = "Please wait..."
      .non_input

© Stack Overflow or respective owner

Related posts about ruby-on-rails